<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目列表</title>
    <style>
        /* 样式可以根据你的需求自定义 */
        ul {
            list-style: none;
            padding: 0;
        }
        li {
            margin-bottom: 10px;
        }
        button {
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <h1>项目列表</h1>
    <ul id="item-list">
        <li>Java <button class="up">上移</button> <button class="down">下移</button> <button class="delete">删除</button></li>
        <li>C <button class="up">上移</button> <button class="down">下移</button> <button class="delete">删除</button></li>
        <li>Python <button class="up">上移</button> <button class="down">下移</button> <button class="delete">删除</button></li>
    </ul>
    <button id="add-item">添加项目</button>

    <script>
        // 获取所有的上移按钮
        const upButtons = document.querySelectorAll(".up");
        upButtons.forEach(button => {
            button.addEventListener("click", () => {
                const listItem = button.parentElement;
                if (listItem.previousElementSibling) {
                    listItem.parentNode.insertBefore(listItem, listItem.previousElementSibling);
                }
            });
        });

        // 获取所有的下移按钮
        const downButtons = document.querySelectorAll(".down");
        downButtons.forEach(button => {
            button.addEventListener("click", () => {
                const listItem = button.parentElement;
                if (listItem.nextElementSibling) {
                    listItem.parentNode.insertBefore(listItem.nextElementSibling, listItem);
                }
            });
        });

        // 获取所有的删除按钮
        const deleteButtons = document.querySelectorAll(".delete");
        deleteButtons.forEach(button => {
            button.addEventListener("click", () => {
                const listItem = button.parentElement;
                listItem.remove();
            });
        });

        // 添加新项目
        const addItemButton = document.getElementById("add-item");
        addItemButton.addEventListener("click", () => {
            const newItem = document.createElement("li");
            newItem.textContent = "新项目";
            newItem.innerHTML += ' <button class="up">上移</button> <button class="down">下移</button> <button class="delete">删除</button>';
            document.getElementById("item-list").appendChild(newItem);
        });
    </script>
</body>
</html>
